package com.lc.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.lc.entity.TemplateParam;

import java.util.Map;

/**
 * Created by 下雨不带伞 on 2020/2/13.
 */
public class SMSUtils {
    public static String sendSms(String signname, String tcode, String mobile, TemplateParam templateParam) throws Exception {

        //DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FjRfd7B1yyva2RRnW2A", "D6Vf9hZKrlnuQ8kz9V3IhgUt2BJbzg");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fq8k4AyaLSYrHxNHNJg", "2oIxCeB6Xy1NIMpkGj9UXesPIw00NO");

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signname);
        //request.putQueryParameter("TemplateCode", "SMS_150505312");
        request.putQueryParameter("TemplateCode", tcode);
        request.putQueryParameter("TemplateParam", new Gson().toJson(templateParam));

        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        System.out.println(data);

        return data;
    }

    public static String sendSms(Map<String,Object> params) throws Exception {
        if (params == null) {
            return "";
        }
        String signname = params.get("signname") == null?"":params.get("signname").toString();
        String tcode = params.get("tcode") == null?"":params.get("tcode").toString();
        String mobile = params.get("mobile") == null?"":params.get("mobile").toString();
        TemplateParam templateParam  = (TemplateParam) params.get("templateParam");
        //DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FjRfd7B1yyva2RRnW2A", "D6Vf9hZKrlnuQ8kz9V3IhgUt2BJbzg");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fq8k4AyaLSYrHxNHNJg", "2oIxCeB6Xy1NIMpkGj9UXesPIw00NO");

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signname);
        //request.putQueryParameter("TemplateCode", "SMS_150505312");
        request.putQueryParameter("TemplateCode", tcode);
        request.putQueryParameter("TemplateParam", new Gson().toJson(templateParam));

        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        System.out.println(data);

        return data;
    }
}
